package Testclass.ajaxTest;

import Model.Student;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/ajax3")
public class ajax3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<Student> stus = new ArrayList<>();
        Student stu1 = new Student();
        stu1.setId(12);
        stu1.setName("aa");
        stu1.setSex("女");
        stu1.setAge(12);
        stu1.setMajor("fule");
        stu1.setTime("555");
        Student stu2 = new Student();
        stu2.setId(13);
        stu2.setName("ll");
        stu2.setSex("女");
        stu2.setAge(13);
        stu2.setMajor("fule");
        stu2.setTime("5565");
        Student stu3 = new Student();
        stu3.setId(56);
        stu3.setName("jj");
        stu3.setSex("女");
        stu3.setAge(20);
        stu3.setMajor("fule");
        stu3.setTime("566");
        stus.add(stu1);
        stus.add(stu2);
        stus.add(stu3);

        String res = JSON.toJSONString(stus);
        System.out.println("后端json格式的字符串 = " + res);

        req.setCharacterEncoding("utf-8");
       resp.setContentType("application/json");

        PrintWriter writer = resp.getWriter();
        writer.write(res);
       writer.flush();
       writer.close();
    }
}
